const api = require('../../common/api');


const fabric_query = require('../../common/service/fabric_query');
const fabric_util = require('../../common/service/fabric_util');
const myJson = require('../../common/json');


module.exports = {

    list: async function (ctx) {
        /*if (!api.isset(ctx.request.query, ["DEDKey","DEEKey"])) {  // lionel 20180820 先入一个参数，传过来的时候，一个参数都没有，就不check了。
        //if (!api.isset(ctx.request.query, ["DEDKey"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }*/
        let condition ={};
        condition.Table = "de";
        condition.DEDKey = ctx.request.query.DEDKey;
        condition.DEEKey = ctx.request.query.DEEKey;    //lionel，因为链码里有需要，所以写了个固定值
        if(condition.DEEKey == "" || condition.DEDKey == "")
        {
            condition.DEDKey = "1536658519";
            condition.DEEKey = "1536658519";     // 因为链码里有需要，所以写了个固定值
        }
        
        let args = [JSON.stringify(condition)];
        let obj = fabric_util.getObj("department", "DESelect", args)

        let res = await fabric_query.query(obj).then((res) => {
            return res;
        });
        if (myJson.isJSON(res)) {
            ctx.body = {
                "error_code": 0,
                "data": {
                    "list":JSON.parse(res)
                }
            }
        } else {
            ctx.body = {
                "error_code": 20000,
                "error_msg": res
            }
        }

    },
    add: async function (ctx) {
        let param = {};
        if (!api.isset(ctx.request.body, ["DEDKey","DEEKey"])) {  // lionel
        //if (!api.isset(ctx.request.body, ["DEDKey"])) {
            ctx.body = {"error_code": 20000, "error_msg": "parameters is not enough!"};
            return;
        }
        param.DEDKey = ctx.request.body.DEDKey;
        param.DEEKey = ctx.request.body.DEEKey;       // lionel
        //condition.DEEKey = "1569405412";     // 因为链码里有需要，所以写了个固定值
        param.table  = "de";
        if (ctx.request.body.Desc!=undefined&&ctx.request.body.Desc!=""){  // 有写Desc，前端没有传过来，说明字段大小写没有匹配上
            param.Desc = ctx.request.body.Desc;
        }

        let args = [JSON.stringify(param)];
        let obj = fabric_util.getObj("department", "DEAdd", args);

        let res = await fabric_query.invoke(obj).then((res) => {
            return res;
        });
        ctx.body = {
            error_code: 0,
            data: res
        }
    }


};